import Vue from 'vue'
import { login, getInfo, logout, loginByUsername, register } from '@/api/login'
import { ACCESS_TOKEN, MERCHANT_ID } from '@/store/mutation-types'
import { welcome, encryption } from '@/utils/util'
import { getStore, setStore } from '@/utils/store'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {},
    access_token: getStore({
      name: 'access_token'
    }) || ''
  },

  mutations: {
    SET_ACCESS_TOKEN: (state, access_token) => {
      state.access_token = access_token
      setStore({
        name: 'access_token',
        content: state.access_token,
        type: 'session'
      })
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          const result = response.result
          Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
          commit('SET_TOKEN', result.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 根据用户名登录
    LoginByUsername ({ commit }, userInfo) {
      const user = encryption({
        data: userInfo,
        key: 'pigxpigxpigxpigx',
        param: ['password']
      })
      return new Promise((resolve, reject) => {
        loginByUsername(user.username, user.password, user.code, user.randomStr).then(response => {
          console.log(response)
          const data = response
          console.log(data)
          commit('SET_ACCESS_TOKEN', data.access_token)
          Vue.ls.set(ACCESS_TOKEN, data.access_token, 7 * 24 * 60 * 60 * 1000)
          commit('SET_TOKEN', data.access_token)
          resolve(response)
        }).catch(error => {
          console.log(error)
          //reject(error)
          resolve(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const result = response.data
          console.log(result)
          if (result.roles) {
            commit('SET_ROLES', result.roles)
            commit('SET_INFO', result)
          } else {
            reject(new Error('getInfo: roles must be a non-null array !'))
          }

          commit('SET_NAME', { name: result.sysUser.username, welcome: welcome() })
          commit('SET_AVATAR', result.sysUser.avatar)
          Vue.ls.set(MERCHANT_ID, result.sysUser.ecoUserNo, 7 * 24 * 60 * 60 * 1000)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        logout(state.token).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        }).finally(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          Vue.ls.remove(ACCESS_TOKEN)
        })
      })
    },

    //注册
    Register ({ commit }, userInfo) {
      console.log("vuex Register")
      console.log(userInfo)
      const user = encryption({
        data: userInfo,
        key: 'pigxpigxpigxpigx',
        param: ['password']
      })
      console.log("vuex Register")
      //userInfo.password = user.password
      return new Promise((resolve, reject) => {
        console.log(userInfo)
        register(userInfo).then(response => {
          console.log(response)
          const data = response
          console.log(data)
          resolve(response)
        }).catch(error => {
          console.log(error)
          //reject(error)
          resolve(error)
        })
      })
    }

  }
}

export default user
